trigger AssignLeadToCampaignULA on Lead (before insert, after insert, before update) {
  
  List<ULACampaigns__c> CustomSettings = [SELECT Web_to_Lead__c, Version__c, User_IP__c, 
                                            Size__c, Section__c, Outlet__c, Landing_Page__c, 
                                            Keyword__c, Extra__c, Employer__c, Campaign_Code__c,
                                            CampaignID__c, Name
                                            FROM ULACampaigns__c];
  integer DefaultPartner = -1;
  integer DefaultGeneric = -1;
 
  integer i=0;
  for (ULACampaigns__c CS: CustomSettings)
     {
       if(CS.Name == 'Default Generic Campaign')
       {
         DefaultGeneric = i;
       }
       if(CS.Name == 'Default Partner Campaign')
       {
         DefaultPartner = i;
       }
       i++;
     }
     
     if (DefaultGeneric == -1 && DefaultPartner <> -1){
       DefaultGeneric = DefaultPartner;
     }
     if (DefaultGeneric <> -1 && DefaultPartner == -1){
       DefaultPartner = DefaultGeneric ;
     }
     
  for (Lead l : Trigger.new) {       
        
       boolean done = false;
         if(l.Web_to_Lead__c == 'WebRFI' ){
           //this is a web to lead -- process this record 
       
                
       if(trigger.isafter){     
          
          // scroll through RFI Web to Lead custom settings to see if we have a match
          
         boolean match = true;  
         boolean CampaignAssigned = false;
         
       if(CustomSettings.size() > 0){
         for (ULACampaigns__c CS: CustomSettings){
           if (CampaignAssigned  == false){
            if(match == true){
                  if( l.RFI_Version__c <> CS.Version__c ){
                  match = false;
                  }
            }
            
            if(match == true){
                  if( l.RFI_Size__c <> CS.Size__c ){
                  match = false;
                  }
            }            
                
            if(match == true){
                  if( l.RFI_Section__c <> CS.Section__c ){
                  match = false;
                  }
            }            
                
            if(match == true){
                  if( l.RFI_Outlet__c <> CS.Outlet__c ){
                  match = false;
                  }
            }            
                
            if(match == true){
                  if( l.RFI_Keyword__c <> CS.Keyword__c ){
                  match = false;
                  }
            }
            
           if(l.RFI_Campaign_Code__c <> 'CEL-RFI-GENERIC')  {   
              if(match == true){
                    if( l.RFI_Employer__c <> CS.Employer__c ){
                    match = false;
                    }
              }
            }            
                
            if(match == true){
                  if( l.RFI_Outlet__c <> CS.Outlet__c ){
                  match = false;
                  }
            }            
       
           
            if(match == true){
                  if( l.RFI_Campaign_Code__c <> CS.Campaign_Code__c ){
                  match = false;
                  }
            }            
             
            if(match == true){
                  if( l.RFI_Extra__c <> CS.Extra__c ){
                  match = false;
                  }
            }
            if(match) {
                CampaignMember CM = new CampaignMember();
                CM.CampaignId = CS.CampaignID__c;
                CM.LeadID = l.id;
                //CM.Status = l.Campaign_Member_Status_Text__c;
                insert CM;
                CampaignAssigned = true;
            }
            match = true;
         }
         }
         
                 
           if (CampaignAssigned == false){
             if(l.RFI_Campaign_Code__c == 'CEL-RFI-PARTNER')
               {
                   CampaignMember CM = new CampaignMember();
                    CM.CampaignId = CustomSettings[DefaultPartner].CampaignID__c;
                    CM.LeadID = l.id;
                    //CM.Status = l.Campaign_Member_Status_Text__c;
                    insert CM;
                    CampaignAssigned = true;
                }
           else
             {
                   CampaignMember CM = new CampaignMember();
                    CM.CampaignId = CustomSettings[DefaultGeneric].CampaignID__c;
                    CM.LeadID = l.id;
                    //CM.Status = l.Campaign_Member_Status_Text__c;
                    insert CM;
                    CampaignAssigned = true;
             }
      }
           
         }
       }
             
      }
     }
    }